﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.Command;
using Register.BLL;
using Register.DAL;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.DoctorPersonalCenter.ashx
{
    /// <summary>
    /// SchedulingInfoHandler 的摘要说明
    /// </summary>
    public class SchedulingInfoHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action=context.Request["action"];
            #region 获取医生安排信息
            if (action.Equals("get"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<Register.Model.SchedulingInfo> list = new List<Register.Model.SchedulingInfo>();
                list = SchedulingInfoBLL.GetPagedScheduling(row, page, "   Doctor_ID=\'DO20120709002 \' ");  //临时模拟医生

                List<SchedulingInfoJSON> json = new List<SchedulingInfoJSON>();
                foreach (Register.Model.SchedulingInfo sch in list)
                {
                    //此处医生编号为人为复制，实际从session中获取

                    string operate = "<a href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-add\" plain=\"true\" onclick=\"makeSure()\" ><img src='../../images/ok.png'>确认</a>|<a href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-edit\" plain=\"true\" onclick=\"SchAdjustment()\"><img src='../../images/pencil.png' >调整申请</a>";

                    string confirmStatus = "<img src='../../images/bullet_cross.png' />待确认";
                    if (sch.SchedulingInfo_ConfirmStatus.Equals("1"))
                    {
                        confirmStatus = "<img src='../../images/bullet_tick.png' />已确认";

                    }
                    else if (sch.SchedulingInfo_ConfirmStatus.Equals("2")) {

                        confirmStatus = "<img src='../../images/bullet_wrench_red.png'/>申请调整中";
                    }

                    json.Add(new SchedulingInfoJSON(DoctorInfoBLL.GetDepartmentsNameById(sch.Departments_ID), DoctorUserInfoBLL.GetDoctorNameById(sch.Doctor_ID), Convert.ToString(sch.SchedulingInfo_AddTime).Substring(0, 9), confirmStatus, Convert.ToString(sch.SchedulingInfo_DayTime).Substring(0, 9), sch.SchedulingInfo_ID, HospitalInfoBLL.GetParamNameById(sch.SchedulingInfo_OutpatientType),sch.SchedulingInfo_OutpatientType, sch.SchedulingInfo_Recoder, Convert.ToString(sch.SchedulingInfo_RegisteredNumber), sch.SchedulingInfo_Status, HospitalInfoBLL.GetParamNameById(sch.SchedulingInfo_TimeType), sch.SchedulingInfo_TimeType,operate));
                }

                SchedulingInfoListJSON schedulingInfoListJSON = new SchedulingInfoListJSON();
                schedulingInfoListJSON.total = SchedulingInfoBLL.GetSchedulingInfoCount1("  Doctor_ID=\'DO20120709002 \'  ");
                schedulingInfoListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(schedulingInfoListJSON));


            }
            #endregion
            #region 确认操作
            else if (action.Equals("makeSure")) {
                Register.Model.SchedulingInfo scheduling = new Register.Model.SchedulingInfo();
                scheduling.SchedulingInfo_ID=context.Request["id"];
                scheduling.SchedulingInfo_ConfirmStatus = "1";
                scheduling.SchedulingInfo_LastAmendTime = DateTime.Now;
                scheduling.SchedulingInfo_LastAmendPerson = "DoubleZ";  //临时赋值
                if (SchedulingInfoBLL.UpdateScheduling(scheduling))
                {
                    context.Response.Write("ok");
                }
                else {
                    context.Response.Write("no");
                }
            
            
            }
            #endregion
            #region 绑定类型
            else if (action.Equals("timeTye"))
            {
                List<Parameter> list;
                list = ParamterHelper.GetParametersByType("Scheduling_Time");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));
            
            }
            #endregion
            #region 添加调整申请
            else if (action.Equals("add"))
            {
                SchedulingAdjustmentInfo schAdj = new SchedulingAdjustmentInfo();
                schAdj.SchedulingAdjustment_AddTime = DateTime.Now;
                schAdj.SchedulingAdjustment_DayTime = Convert.ToDateTime(context.Request["SchedulingAdjustment_DayTime"]);
                schAdj.SchedulingAdjustment_ID = DataIDHelper.GetDataID("SchedulingAdjustment_ID");
                schAdj.SchedulingAdjustment_Note = context.Request["SchedulingAdjustment_Note"];
                schAdj.SchedulingAdjustment_OutpaientType = context.Request["SchedulingAdjustment_OutpaientType"];
                schAdj.SchedulingAdjustment_Recoder = "DoubleZ"; //临时赋值
                schAdj.SchedulingAdjustment_RegisteredNumber = Convert.ToInt32(context.Request["SchedulingAdjustment_RegisteredNumber"]);
                schAdj.SchedulingAdjustment_Status = "1";
                schAdj.SchedulingAdjustment_TimeTye = context.Request["SchedulingAdjustment_TimeTye"];
                schAdj.SchedulingAdjustment_VerifyStatus = "0";
                schAdj.SchedulingInfo_ID = context.Request["id"];
                JavaScriptSerializer jss1 = new JavaScriptSerializer();
                context.Response.Write(jss1.Serialize(new message(SchedulingInfoBLL.AddSchedulingAdjustmentInfo(schAdj), "申请成功！")));
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class SchedulingInfoListJSON
{

    public int total { get; set; }
    public List<SchedulingInfoJSON> rows { get; set; }

}
class SchedulingInfoJSON
{

    public string SchedulingInfo_ID
    {
        set;
        get;
    }
    public string Departments_ID
    {
        get;
        set;
    }
    public string Doctor_ID
    {
        get;
        set;
    }

    public string SchedulingInfo_TimeType
    {
        get;
        set;
    }
    public string SchedulingInfo_TimeTypeValue
    {
        get;
        set;
    }
    public string SchedulingInfo_DayTime
    {
        get;
        set;
    }
    public string SchedulingInfo_OutpatientType
    {
        get;
        set;
    }
    public string SchedulingInfo_OutpatientTypeValue
    {
        get;
        set;
    }

    public string SchedulingInfo_RegisteredNumber
    {
        get;
        set;
    }
    public string SchedulingInfo_ConfirmStatus
    {
        get;
        set;
    }
    public string SchedulingInfo_Status
    {
        get;
        set;
    }
    public string SchedulingInfo_AddTime
    {
        get;
        set;

    }
        public string SchedulingInfo_Recoder
    {
        get;
        set;

    }
        public string Operate
        {
            get;
            set;
        }
    public SchedulingInfoJSON(string Departments_ID, string Doctor_ID, string SchedulingInfo_AddTime, string SchedulingInfo_ConfirmStatus, string SchedulingInfo_DayTime, string SchedulingInfo_ID, string SchedulingInfo_OutpatientType,string SchedulingInfo_OutpatientTypeValue, string SchedulingInfo_Recoder, string SchedulingInfo_RegisteredNumber, string SchedulingInfo_Status, string SchedulingInfo_TimeType,string SchedulingInfo_TimeTypeValue,string Operate)
    {
       this.Departments_ID=Departments_ID;
        this.Doctor_ID=Doctor_ID;
        this.SchedulingInfo_AddTime=SchedulingInfo_AddTime;
        this.SchedulingInfo_ConfirmStatus=SchedulingInfo_ConfirmStatus;
        this.SchedulingInfo_DayTime=SchedulingInfo_DayTime;
        this.SchedulingInfo_ID=SchedulingInfo_ID;
        this.SchedulingInfo_OutpatientType=SchedulingInfo_OutpatientType;
        this.SchedulingInfo_OutpatientTypeValue = SchedulingInfo_OutpatientTypeValue;
        this.SchedulingInfo_Recoder=SchedulingInfo_Recoder;
        this.SchedulingInfo_RegisteredNumber=SchedulingInfo_RegisteredNumber;
        this.SchedulingInfo_Status=SchedulingInfo_Status;
        this.SchedulingInfo_TimeType=SchedulingInfo_TimeType;
        this.SchedulingInfo_TimeTypeValue = SchedulingInfo_TimeTypeValue;
        this.Operate = Operate;
    }
}